package com.bestcem.xm.common.core.config.delaymq.dao.impl;

import cn.hutool.core.collection.CollUtil;
import com.bestcem.xm.common.core.config.delaymq.dao.MqSendLogDao;
import com.bestcem.xm.common.core.config.delaymq.dao.mapper.MqSendLogMapper;
import com.bestcem.xm.common.core.config.delaymq.entity.MqSendLogDO;
import com.bestcem.xm.common.core.config.delaymq.entity.MqSendLogEntity;
import com.bestcem.xm.common.core.uitls.DateUtil;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * mq消息发送日志
 *
 * @author sh.pei <sh.pei@idiaoyan.com>
 * @version v1.0
 * @date 2021/8/6 11:06
 */
@Repository
public class MqSendLogImpl implements MqSendLogDao {

    @Resource
    private MqSendLogMapper mqSendLogMapper;

    @Value("${spring.application.name}")
    private String applicationName;

    @Override
    public String insert(MqSendLogDO mqSendLogDO) {
        MqSendLogEntity entity = toEntity(mqSendLogDO);
        if (Objects.isNull(entity.getSenderId())) {
            String id = new ObjectId().toHexString();
            entity.setSenderId(id);
        }
        entity.setCreateTime(DateUtil.getCommonDate());
        entity.setUpdateTime(DateUtil.getCommonDate());
        entity.setApplicationName(applicationName);
        mqSendLogMapper.insert(entity);
        return entity.getSenderId();
    }

    @Override
    public List<MqSendLogDO> listAwardMqSendLogs(Integer status, Date startDelayTime, Date endDelayTime, int batchQueryCount) {
        List<MqSendLogEntity> awardMqSendLogEntityList = mqSendLogMapper.listMqSendLogs(status, startDelayTime, endDelayTime, batchQueryCount, applicationName);
        return awardMqSendLogEntityList.stream().map(this::toDO).collect(Collectors.toList());
    }

    @Override
    public Integer updateStatusById(String id, Integer status) {
        return mqSendLogMapper.updateStatusById(id, status, DateUtil.getCommonDate());
    }

    @Override
    public Integer batchUpdateStatus(List<String> ids, int countModify, int newStatus, Date updateTime) {
        return mqSendLogMapper.batchUpdateStatus(ids, countModify, newStatus, updateTime);
    }

    @Override
    public List<MqSendLogDO> listByIds(List<String> ids) {
        if (CollUtil.isEmpty(ids)) {
            return new ArrayList<>();
        }
        List<MqSendLogEntity> sendLogEntities = mqSendLogMapper.listByIds(ids);
        if (CollUtil.isNotEmpty(sendLogEntities)) {
            return sendLogEntities.stream().map(this::toDO).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    @Override
    public int deleteMqSendLogs(List<Integer> statuses, Date dateTime) {
        return mqSendLogMapper.deleteMqSendLogs(statuses, dateTime, applicationName);
    }


    private MqSendLogDO toDO(MqSendLogEntity entity) {
        MqSendLogDO mqSendLogDO = new MqSendLogDO();
        mqSendLogDO.setId(entity.getSenderId());
        //mqSendLogDO.setExchangeName(entity.getExchangeName());
        //mqSendLogDO.setRoutingKey(entity.getRoutingKey());
        mqSendLogDO.setApplicationName(entity.getApplicationName());
        mqSendLogDO.setBindingName(entity.getBindingName());
        mqSendLogDO.setSendMsg(entity.getSendMsg());
        mqSendLogDO.setStatus(entity.getStatus());
        mqSendLogDO.setCount(entity.getCount());
        mqSendLogDO.setDelayTime(entity.getDelayTime());
        mqSendLogDO.setCreateTime(entity.getCreateTime());
        mqSendLogDO.setUpdateTime(entity.getUpdateTime());
        return mqSendLogDO;
    }

    private MqSendLogEntity toEntity(MqSendLogDO mqSendLogDO) {
        MqSendLogEntity entity = new MqSendLogEntity();
        entity.setSenderId(mqSendLogDO.getId());
        //entity.setExchangeName(mqSendLogDO.getExchangeName());
        //entity.setRoutingKey(mqSendLogDO.getRoutingKey());
        entity.setApplicationName(mqSendLogDO.getApplicationName());
        entity.setBindingName(mqSendLogDO.getBindingName());
        entity.setCount(mqSendLogDO.getCount());
        entity.setSendMsg(mqSendLogDO.getSendMsg());
        entity.setStatus(mqSendLogDO.getStatus());
        entity.setDelayTime(mqSendLogDO.getDelayTime());
        return entity;
    }
}
